package cn.cycleif.app.service;

import cn.cycleif.app.domain.dto.VoteDTO;
import cn.cycleif.app.mapper.VoteMapper;
import cn.cycleif.entity.Vote;
import cn.cycleif.enums.CommonStatusEnum;
import cn.cycleif.enums.ResultCodeEnum;
import cn.cycleif.enums.VoteStatusEnum;
import cn.cycleif.util.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @Date: 25/2/13-12:59
 * @Desc:
 */
@Service
@RequiredArgsConstructor
public class VoteService extends ServiceImpl<VoteMapper, Vote> {

  private final VoteMapper   voteMapper;
  private final RedisService redisService;

  public R<String> addVote(VoteDTO dto) {
    if (dto.getStatus() == dto.getLastStatus()) return R.error(ResultCodeEnum.ERROR_REQUEST_FORMAT);
    if (dto.getStatus() == VoteStatusEnum.AGREE
        || (dto.getStatus() == VoteStatusEnum.NOTHING
        && dto.getLastStatus() == VoteStatusEnum.AGREE)) {
      redisService.agreeVote(dto);
      return R.success();
    } else if (dto.getStatus() == VoteStatusEnum.DISAGREE
        || (dto.getStatus() == VoteStatusEnum.NOTHING
        && dto.getLastStatus() == VoteStatusEnum.DISAGREE)) {
      redisService.disagreeVote(dto);
      return R.success();
    }
    return R.error(ResultCodeEnum.ERROR_REQUEST_FORMAT);
  }
}
